<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        v-loading="loading"
        :expand-on-click-node="false"
        :data="depts"
        :props="defaultProps"
        default-expand-all
      >
        <template v-slot="{ data }">
          <el-row
            style="width: 100%; height: 40px"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span>{{ data.managerName }}</span>
              <!-- 显示区域内容 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>

                <!-- 下拉菜单 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept
      ref="addDept"
      v-model="showDialog"
      :current-node-id="currentNodeId"
      @okAdd="getDepartment"
    />
  </div>
</template>
<script>
import AddDept from './components/add-dept.vue'
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null,
      loading: false // 是否加载状态
    }
  },
  created() {
    // 请求数据
    this.getDepartment()
  },
  methods: {
    // 调数据接口
    async getDepartment() {
      this.loading = true
      const res = await getDepartment()
      // 转化树状数组
      this.depts = transListToTreeData(res, 0)
      this.loading = false
    },
    // 点击下拉菜单执行
    async operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true
        // 把父id转给子组件
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.currentNodeId = id // 记录id 要用它获取数据
        this.$nextTick(async() => {
          await this.$refs.addDept.getDepartmentDetail()
          // 数据更新完成再打开弹窗
          this.showDialog = true
        })
      } else {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delDepartment(id)
        this.$message.success('删除部门成功')
        this.getDepartment()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
